// ignore_for_file: public_member_api_docs, sort_constructors_first
import 'dart:convert';
import 'dart:math';

import 'package:flutter/foundation.dart';
import 'package:hong_vpn_new_20240626/utils/Configs/ai_config.dart';

enum AiLabel {
  middleAge(rawValue: "Middle Age"),
  easygoing(rawValue: "Easygoing");

  final String? rawValue;

  const AiLabel({this.rawValue});

  static AiLabel rawValueBy(String value) {
    switch (value) {
      case "Middle Age":
        return AiLabel.middleAge;
      default:
        return AiLabel.easygoing;
    }
  }

  static List<AiLabel> randoms() {
    var random = Random().nextInt(2);
    var list = AiLabel.values.sublist(0, random);
    return list;
  }

  String get imageUrl => switch (this) {
        AiLabel.middleAge => "assets/images/工作.png",
        _ => "assets/images/笑脸 .png"
      };
}

class AiModel {
  String? title;
  String? header;
  bool isLike;
  int likeNum;
  List<AiLabel> labels;
  String description;

  AiModel(
      {this.title,
      this.header,
      this.isLike = false,
      this.likeNum = 0,
      required this.labels,
      this.description = ""});

  String get likeNumFormatter => _likeNumFormatter();

  String _likeNumFormatter() {
    if (likeNum < 1000) {
      return likeNum.toString();
    }
    double n = likeNum / 1000;

    return "${n.toStringAsFixed(1)}k";
  }

  like() async {
    var num = isLike ? likeNum - 1 : likeNum + 1;
    if (num < 0) {
      num = 0;
    }
    isLike = !isLike;
    AiConfig.reloadCacheAisBy(this);
  }

  AiModel copyWith({
    String? title,
    String? header,
    bool? isLike,
    int? likeNum,
    List<AiLabel>? labels,
    String? description,
  }) {
    return AiModel(
        title: title ?? this.title,
        header: header ?? this.header,
        isLike: isLike ?? this.isLike,
        likeNum: likeNum ?? this.likeNum,
        labels: labels ?? this.labels,
        description: description ?? this.description);
  }

  Map<String, dynamic> toMap() {
    return <String, dynamic>{
      'title': title,
      'header': header,
      'isLike': isLike,
      'likeNum': likeNum,
      'labels': labels.map((x) => x.rawValue ?? "").toList(),
      'description': description,
    };
  }

  factory AiModel.fromMap(Map<String, dynamic> map) {
    var labels = (map['labels'] as List)
        .map((x) => AiLabel.rawValueBy(x as String))
        .toList();

    return AiModel(
      title: map['title'] != null ? map['title'] as String : null,
      header: map['header'] != null ? map['header'] as String : null,
      isLike: map['isLike'] as bool,
      likeNum: map['likeNum'] != null ? map['likeNum'] as int : 0,
      labels: labels,
      description:
          map['description'] != null ? map['description'] as String : "",
    );
  }

  String toJson() => json.encode(toMap());

  factory AiModel.fromJson(String source) =>
      AiModel.fromMap(json.decode(source) as Map<String, dynamic>);

  @override
  String toString() {
    return 'AiModel(title: $title, header: $header, isLike: $isLike, likeNum: $likeNum, labels: $labels, description: $description) ';
  }

  @override
  bool operator ==(covariant AiModel other) {
    if (identical(this, other)) return true;

    return other.title == title &&
        other.header == header &&
        other.isLike == isLike &&
        other.likeNum == likeNum &&
        other.description == description &&
        listEquals(other.labels, labels);
  }

  @override
  int get hashCode {
    return title.hashCode ^
        header.hashCode ^
        isLike.hashCode ^
        likeNum.hashCode ^
        description.hashCode ^
        labels.hashCode;
  }
}
